package com.mmy.controller.restaurant;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.restaurant.TRestaurantBase;
import com.mmy.domain.scenic.VScenicBase;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.restaurant.TRestaurantBaseServiceImpl;
import com.mmy.service.scenic.impl.TScenicBaseServiceImpl;

/**
 * 类的介绍：餐馆信息
 *
 * @author hanxl
 * @date 创建日期：2020-09-14
 * @version 1.0
 */
@Controller
public class TRestaurantBaseController {

	@Autowired
	TRestaurantBaseServiceImpl tRestaurantBaseServiceImpl;
	@Autowired
	TScenicBaseServiceImpl tScenicBaseServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 创建餐馆
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 韩现丽
	 * @date 创建日期2020-09-14
	 */
	@RequestMapping(value = "/tocreaterestaurantbase")
	public String toCreateRestaurantBase(Model model) {

		List<VScenicBase> lstScenicBase = tScenicBaseServiceImpl.findAll();
		model.addAttribute("scenicBaselist", lstScenicBase);
		/* 查询所有区域 国家或者省市区 */
		List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		model.addAttribute("newTConstZone", db_TConstZone);
		List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		model.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);

		return "webbackground/restaurantmanagement/createrestaurantbase";
	}

	@RequestMapping(value = "/savecreaterestaurantbase")
	public String saveCreateRestaurantBase(TRestaurantBase tRestaurantBase, HttpSession session) {
		// 获得最大餐馆编号
		tRestaurantBase.setRestaurantID(common.createBaseCode(UtilConstants.TableName.T_RESTAURANT_BASE));

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		tRestaurantBase.setCreateDate(new Date());

		tRestaurantBase.setCreator(tSysUser.getUserName());
		tRestaurantBaseServiceImpl.save(tRestaurantBase.getRestaurantID());
		tRestaurantBaseServiceImpl.save(tRestaurantBase);
		return "redirect:restaurantindex";

	}

	/**
	 * 函数功能说明：修改餐馆
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 韩现丽
	 * @date 创建日期2020-09-14
	 */

	/* 点击修改按钮进入修改页面 */
	@RequestMapping("/toupdaterestaurantbase")
	public String updateRestaurantBaseCheckedFind(HttpServletRequest request, Model mRestaurant) {
		String iRestaurantID = request.getParameter("iRestaurantID");
		TRestaurantBase db_TRestaurantBase = tRestaurantBaseServiceImpl
				.findByRestaurantID(Integer.parseInt(iRestaurantID));
		mRestaurant.addAttribute("newTRestaurantBase", db_TRestaurantBase);
		List<VScenicBase> lstScenicBase = tScenicBaseServiceImpl.findAll();
		mRestaurant.addAttribute("scenicBaselist", lstScenicBase);
		/* 查询所有区域 国家或者省市区 */
		List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
		mRestaurant.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		mRestaurant.addAttribute("newTConstZone", db_TConstZone);
		List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		mRestaurant.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);

		return "webbackground/restaurantmanagement/updaterestaurantbase";
	}

	/* 提交修改的餐馆信息 */
	@RequestMapping(value = "/saveupdaterestaurantbase")
	public String updateRestaurantBase(TRestaurantBase tRestaurantBase, HttpSession session) throws Exception {
		int iRestaurantID = tRestaurantBase.getRestaurantID();
		/* 定义一个新的实体类对象 */
		TRestaurantBase dtRestaurantBase = tRestaurantBaseServiceImpl.findByRestaurantID(iRestaurantID);
		dtRestaurantBase.setRestaurantName(tRestaurantBase.getRestaurantName());
		dtRestaurantBase.setScenicID(tRestaurantBase.getScenicID());
		dtRestaurantBase.setZoneID(tRestaurantBase.getZoneID());
		dtRestaurantBase.setAddress(tRestaurantBase.getAddress());
		dtRestaurantBase.setContact(tRestaurantBase.getContact());
		dtRestaurantBase.setTel(tRestaurantBase.getTel());
		dtRestaurantBase.setRate(tRestaurantBase.getRate());
		dtRestaurantBase.setNumber(tRestaurantBase.getNumber());
		dtRestaurantBase.setRoom(tRestaurantBase.getRoom());
		dtRestaurantBase.setIntro(tRestaurantBase.getIntro());
		dtRestaurantBase.setImgPath(tRestaurantBase.getImgPath());
		dtRestaurantBase.setMemo(tRestaurantBase.getMemo());
		dtRestaurantBase.setModifyDate(new Date());
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		dtRestaurantBase.setReviser(tSysUser.getUserName());

		tRestaurantBaseServiceImpl.save(dtRestaurantBase);

		return "redirect:restaurantindex";

	}

	/**
	 * 函数功能说明： 删除酒店信息
	 *
	 * @param 参数说明 tHotelBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.08.18
	 */
	@PostMapping("/deleterestaurantbasechecked")
	@ResponseBody
	public String deleteRestaurantBaseChecked(String checkList) {
		String[] strRestaurantIDs = checkList.split(",");
		List<Integer> iRestaurantIDs = new ArrayList<>();

		for (String strRestaurantID : strRestaurantIDs) {
			iRestaurantIDs.add(Integer.parseInt(strRestaurantID));
		}

		tRestaurantBaseServiceImpl.deleteChecked(iRestaurantIDs);

		return "redirect:restaurantindex";

	}
}
